package com.zllkj.mm.util;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.view.WindowManager;

public class Authority {
 
    private static final int MY_PERMISSION_REQUEST_CODE = 10000;
    //申请的权限，字符串模式
    public static final String CALL_PHONE = Manifest.permission.CALL_PHONE;//打电话权限
    public static final String CAMERA = Manifest.permission.CAMERA;//相机摄像头权限
    public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;//读取内存卡权限
    public static final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;//写入内存卡权限
 
    //申请的权限，数组模式
    public static final String[] CALL_PHONE_ARR = {Manifest.permission.CALL_PHONE};//打电话权限
    public static final String[] CAMERA_ARR = {Manifest.permission.CAMERA};//相机摄像头权限
    public static final String[] READ_EXTERNAL_STORAGE_ARR = {Manifest.permission.READ_EXTERNAL_STORAGE};//读取内存卡权限
    public static final String[] WRITE_EXTERNAL_STORAGE_ARR = {Manifest.permission.WRITE_EXTERNAL_STORAGE};//写入内存卡权限
 
    //权限数组
    public static final String[] str = {
//            Manifest.permission.CALL_PHONE,//打电话
            Manifest.permission.CAMERA, //相机
//            Manifest.permission.ACCESS_NETWORK_STATE,//网络
//            Manifest.permission.ACCESS_WIFI_STATE,//wifi
//            Manifest.permission.CHANGE_WIFI_STATE,//wifi
//            Manifest.permission.INTERNET,//网络
            Manifest.permission.READ_EXTERNAL_STORAGE,//获取sd
            Manifest.permission.WRITE_EXTERNAL_STORAGE//写入sd
    };
 
    //申请权限,多项申请(Activity)
    public static void CallMobile(Activity mContext) {
        ActivityCompat.requestPermissions(
                mContext,
                str,
                MY_PERMISSION_REQUEST_CODE//回调的标识
        );
    }
 
    //申请权限,单项权限,(Activity)
    public static void CallMobileArr(Activity mContext, String[] str) {
        ActivityCompat.requestPermissions(
                mContext,
                str,
                MY_PERMISSION_REQUEST_CODE//回调的标识
        );
    }
 
    //申请权限（fragment）
    //  requestPermissions(Authority.CAMERA_ARR, 10000);这种模式，不可以显示申请权限弹框.如果禁止所申请的权限，可以显示自定义弹框
    //下面这种模式：可以显示申请权限弹框.如果禁止所申请的权限，并且“下次不会再次提醒”已勾选，不会显示弹框
    @TargetApi(Build.VERSION_CODES.M)
    public static void CallMobileFragment(Activity mContext, String[] str) {
        mContext.requestPermissions(str, MY_PERMISSION_REQUEST_CODE);
    }
 
 
    //判断权限是否申请通过
    public static boolean lacksPermission(Context mContexts, String permission) {
        return ContextCompat.checkSelfPermission(mContexts, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
 
 
    /**
     * @param s                  提示语
     * @param applicationContext 上下文
     * @param type               类型，是否关闭页面 1关闭，0不关闭
     */
    public static void openAppDetails(String s, final Activity applicationContext, final int type) {
        final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(applicationContext);
        builder.setMessage(s);
        builder.setCancelable(false);
        builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + applicationContext.getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                applicationContext.startActivity(intent);
                if (type == 1) {
                    applicationContext.finish();
                }
 
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
 
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (type == 1) {
                    applicationContext.finish();
                }
            }
        });
        builder.show();
    }
 
    //设置popu背景色
    public static void setBackgroundAlpha(float bgAlpha, Activity mContext) {
        WindowManager.LayoutParams lp = mContext.getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        mContext.getWindow().setAttributes(lp);
    }
}